/*
 * public class Billboard
 * author: adam barney
 * Billboard is the model class that serves as the public accessor to the billboard data
 * object, as well as the generic billboard bean.
 */

/*
 * This file is part of WhatItDo.
 *
 * WhatItDo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WhatItDo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WhatItDo.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.sourceforge.whatitdo.billboard.model;

import java.util.ArrayList;
import java.awt.*;
import java.awt.image.*;

// import java.io.*;

public class Billboard {
    // constructor for Billboard
    public Billboard() {

    }

    String id = null;
    String name = null;
    String tickerText = new String();
    ArrayList<String> slideshowText = new ArrayList<String>();
    String imageName = new String();
    int imageHeight;
    int imageWidth;
    BufferedImage image = new BufferedImage(256, 288,
	    BufferedImage.TYPE_INT_ARGB);
    boolean isPublished=false;

    public String getId() {
	return id;
    }

    public void setId(String id) {
	if (DataService.isUniqueBillboardId(id)) {
	    this.id = id;
	} else {
	    /*
	     * TODO: figure out what the hell I'm supposed to do if somebody
	     * sends me a nonunique id.
	     */
	}
    }

    // public String getName() {
    // return name;
    // }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	if (name.length() > 255) {
	    name.substring(0, 255);
	}
	this.name = name;
    }
    
    public boolean isPublished() {
        return isPublished;
    }

    public void setPublished(boolean isPublished) {
        this.isPublished = isPublished;
    }

    public String getTickerText() {
	return tickerText;
    }

    public void setTickerText(String tickerText) {
	if (tickerText.length() > 255) {
	    tickerText.substring(0, 255);
	}
	this.tickerText = tickerText;
    }

    public ArrayList<String> getSlideshowText() {
	return slideshowText;
    }

    public void setSlideshowText(ArrayList<String> slideshowText) {
	for (int i = 0; i < slideshowText.size(); i++) {
	    String text = new String();
	    text = slideshowText.get(i);
	    if (text.length() > 255) {
		text.substring(0, 254);
	    }
	    slideshowText.set(i, text);
	}
	this.slideshowText = slideshowText;
    }

    /*
     * TODO: figure out the best way to store images. I don't know if
     * BufferedImages are the best way to go.
     */
    public String getImageName() {
	return imageName;
    }

    public void setImageName(String imageName) {
	this.imageName = imageName;
    }

    public int getImageHeight() {
	return imageHeight;
    }

    public void setImageHeight(int imageHeight) {
	this.imageHeight = imageHeight;
    }

    public int getImageWidth() {
	return imageWidth;
    }

    public void setImageWidth(int imageWidth) {
	this.imageWidth = imageWidth;
    }

    public BufferedImage getImage() {
	return image;
    }

    public void setImage(BufferedImage image) {
	this.image = image;
    }

    /*
     * Write this completed billboard to the database.
     */
    public boolean saveBillboard() {
	System.out.println("Billboard.saveBillboard has been called.");
	DataService.writeBillboard(this);

	return true;
    }

    /*
     * Grab a billboard from DataService and make it active. TODO: set the
     * image. TODO: come up with a way to validate whether or not this
     * succeeded, so the boolean return actually makes some sense.
     */
    public boolean loadBillboard(String id) {
	this.id = id;
	this.name = DataService.getName(id);
	this.slideshowText = DataService.getSlideshowText(id);
	this.tickerText = DataService.getTickerText(id);
	this.isPublished = DataService.isPublished(id);
	return true;

    }

    /*
     * STATIC METHODS ARE HERE FOR CONVENIENCE (HACKS) DO NOT USE THESE METHODS
     * UNLESS YOU LIKE BUGGY SOFTWARE.
     */

    /*
     * Static method that is called from ChooseBillboardBean. Returns a list of
     * all billboards in the system.
     */
    // public static ArrayList<String> getAllNames() {
    // ArrayList<String> rtrn = DataService.getAllNames();
    // return rtrn;
    //
    // }
    /*
     * Static method that calls getIdFromName.
     */
    public static String getIdFromName(String name) {
	String rtrn = DataService.getBillboardIdFromName(name);
	return rtrn;
    }

    public static String getStaticTickerText(String id) {
	String rtrn = DataService.getTickerText(id);
	return rtrn;
    }

    /*
     * This method exists only for the purpose of testing. Don't use it.
     */
    public String testGetName(String id) {
	String rtrn = new String();
	rtrn = DataService.getName(id);
	return rtrn;
    }

    public void createNew(String newId, String user) {

	DataService.createNewBillboard(newId, user);
	this.id = newId;
	this.name = newId;

    }

   

}
